package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.personal.enuminterface.UserStatus;
import com.zyd.blog.persistence.mapper.UserFansMapper;
import com.zyd.blog.personal.service.UserFocusService;
import com.zyd.blog.personal.service.UserNoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户关注信息业务逻辑层
 * @author mfy
 */
@Service
public class UserFocusServiceImpl implements UserFocusService {

    @Resource
    private UserFansMapper userFansMapper;

    @Resource
    private UserNoService userNoService;

    private Map<String,Object> getCondition(Long userId,Long focusId){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("userId", userId);
        condition.put("focusId", focusId);
        return condition;
    }

    @Override
    public  Boolean isHaveFocus(Long id,Long userId,Long focusId,String isFocus){
        Map<String,Object> condition = getCondition(userId, focusId);
        condition.put("isFocus", isFocus);
        List<Map<String,Object>> focus = userFansMapper.queryUserFansByCondition(condition);
        return focus != null && focus.size() != 0;
    }

    /**
     * 得到关注对象
     * @param userId    用户id
     * @return  关注列表
     */
    @Override
    public List<Map<String,Object>> getUserFocusInfo(Long userId,Long focusId,String isFocus){
        //  2. 查询用户关注
        Map<String,Object> condition = new HashMap<>(16);
        // 用户自身是否存在
        condition.put("userIsDelete", IsDelete.IS_DELETE_NO);
        // 用户自身状态是否正常
        condition.put("userStatus", UserStatus.USER_STATUS_DEFAULT);
        // 是否关注
        condition.put("isFocus", isFocus);
        // 查询条件
        condition.put("userId",userId);
        condition.put("focusId",focusId);
        return userFansMapper.queryUserFansByCondition(condition);
    }

    /**
     * 关注
     * @param userId    用户id
     * @param focusUserId   关注对象id
     * @return  关注结果
     */
    @Override
    public int focusOtherUser(Long userId,Long focusUserId){
        if(userNoService.getUserNoByCondition(userId,IsDelete.IS_DELETE_NO,Status.STATUS_YES).size() == 0){
            // 用户不存在或者被冻结
            return 1;
        }
        if(userNoService.getUserNoByCondition(focusUserId,IsDelete.IS_DELETE_NO,Status.STATUS_YES).size() == 0){
            // 关注对象不存在或者被冻结
            return 2;
        }
        Map<String,Object> condition = getCondition(userId, focusUserId);
        List<Map<String,Object>> focus = userFansMapper.queryUserFansByCondition(condition);
        if(focus == null || focus.size() == 0){
            condition.put("updateTime", (new Date()).getTime() );
            condition.put("addTime", (new Date()).getTime() );
            if(userFansMapper.addUserFans(condition) > 0){
                // 关注成功
                return 0;
            }
        }else{
            String isFocus = "isFocus";
            if( Status.STATUS_YES.equals(focus.get(0).get(isFocus))){
                // 已经关注,无需再次关注
                return 3;
            }else{
                condition.put("status", Status.STATUS_YES );
                condition.put("updateTime", (new Date()).getTime() );
                if(userFansMapper.updateUserFans(condition) > 0){
                    // 重新关注成功
                    return 0;
                }
            }
        }
        return -1;
    }

    /**
     * 取消关注
     * @param userId    用户id
     * @param focusUserId   关注对象id
     * @return  操作结果
     */
    @Override
    public int noFocus(Long userId,Long focusUserId){
        if(userNoService.getUserNoByCondition(userId,IsDelete.IS_DELETE_NO,Status.STATUS_YES).size() == 0){
            // 用户不存在或者被冻结
            return 1;
        }
        Map<String,Object> condition = getCondition(userId, focusUserId);
        List<Map<String,Object>> focus = userFansMapper.queryUserFansByCondition(condition);
        if(focus == null || focus.size() == 0){
            //  并未关注
            return 2;
        }else{
            String isFocus = "isFocus";
            if( Status.STATUS_YES.equals(focus.get(0).get(isFocus))){
                condition.put("status", Status.STATUS_NO );
                condition.put("updateTime", (new Date()).getTime() );
                if(userFansMapper.updateUserFans(condition) > 0){
                    // 取消关注成功
                    return 0;
                }
            }else{
                //  并未关注
                return 2;
            }
        }
        return -1;
    }
}
